#### Recode data from Alvarez et al (2019 before analysis) ####

#### Packages ####
library(tidyverse) # data wrangling
library(here) # relative path management

#### Load data ####
load(here("RecodedData.RData"))

# give shorter name
dle = RecodedData

#### Explore ####
# Attention checks
table(dle$type.short) # 1 attentive, 0 inattentive.

# This is general attention, an alternative if those who fail and passed the first check
# since the DLE comes after this but before the second trap question
table(dle$failTQ1)

# treatment variables
table(dle$treat)

# outcomes
summary(dle$listA)

summary(dle$listB)

# There is missing data on the outcomes, it looks like people were removed from the list experiment if they missed the first trap question:
with(dle, table(failTQ1, is.na(listA)))

with(dle, table(failTQ1, is.na(listB)))

with(dle, table(type.short, is.na(listA)))

with(dle, table(type.short, is.na(listB)))

#### Recode variables ####

# Separate treatment and the presence of the sensitive trait
# Do two things:
# - Create indicator that distinguishes whether respondent is assigned to organization X or Y in treatment
# - Create indicator that distinguishes whether sensitive item appears on list A or B
dle = dle %>% 
  mutate(experiment = ifelse(treat == "C_listA, T_OrgX_listB" |
                               treat == "T_OrgX_listA, C_listB", "X", "Y"),
         sensitive = ifelse(treat == "T_OrgX_listA, C_listB" |
                              treat == "T_OrgY_listA, C_listB", "A", "B"),
         trt_A = ifelse(sensitive == "A", 1, 0),
         trt_B = ifelse(sensitive == "B", 1, 0))

with(dle, table(treat, experiment))

with(dle, table(treat, sensitive))

#### Subset to relevant variables ####
dle = dle %>% 
  filter(failTQ1 == 0) %>% 
  select(experiment, sensitive, trt_A, trt_B, listA, listB, type.short, female, f.educ, educ,
         f.agecat, age, f.region)

#### Save ####
save(dle, file = here("attn_rep.RData"))
